const calc = _curryHeight(_addHeight);
const app = getApp();

function _curryHeight(fn) {
  let height = [];
  return function () {
    if (arguments.length === 0) {
      let copy = height.slice(0);
      height = [];
      return fn.apply(null, copy);
    } else {
      height = height.concat([...arguments]);
    }
  };
}

function _addHeight() {
  let count = 0;
  for (let i = 0, l = arguments.length; i < l; i++) {
    count += arguments[i];
  }
  return count;
}
function calcScrollHeight(domId = [], gap = 40) {
  let dom = my.createSelectorQuery();
  let num = 0;
  if (domId.length) {
    domId.forEach((i) => {
      dom.select(i).boundingClientRect();
    });
    return new Promise((resolve) => {
      dom.exec((res) => {
        calc(
          ...res.map((r) => {
            if (r) {
              return r.height;
            }
            return 0;
          }),
        );
        num = app.globalData.systemInfo.windowHeight - Math.floor(calc());
        resolve(num - gap);
      });
    });
  }
  return 0;
}

export default calcScrollHeight;
